﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Xml;
using System.Xml.Xsl;
using System.Data;

public partial class KMrpt20 : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            InsActLog(mid, "浏览");
            Session["ResultData"] = null;
            //BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
            this.txtstatus.GetStore().DataSource = db.DM_GetDateSet("select stateid,cnstatename as statename from Sys_State where statefixflag='auditresult'");
            txtvendor.GetStore().DataSource = db.DM_GetDateSet("select vendcustid,vendcustname from Bas_InterCompany order by vendcustname");
            this.txtseason.GetStore().DataSource = mydb.DM_GetDateSet("select distinct seasonname from v_mat");
            this.txtyear.GetStore().DataSource = mydb.DM_GetDateSet("select distinct yearno from v_mat order by yearno");
            this.txtmatkind.GetStore().DataSource = mydb.DM_GetDateSet("select distinct kindname from v_mat");
            this.cb_sex.GetStore().DataSource = db.DM_GetDateSet("select sexname from sd_mat_sex");
            this.txtbdate.Text = DateTime.Now.ToString("yyyy-MM-01");
            this.txtedate.Text = DateTime.Now.ToString();
            //this.txtbjhdate.Text = DateTime.Now.ToString("yyyy-MM-01");
            //this.txtejhdate.Text = DateTime.Now.ToString(); 
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        try
        {
            this.Store1.DataSource = QureyData();
            this.Store1.DataBind();
        }
        catch (Exception ex)
        {
            ShowBox("查询出错：" + ex.Message);
        }
    }

    protected DataSet QureyData()
    {
        try
        {
            string bdate, edate, sjhdaate, ejhdaate, sdhdate, edhdate, srkdate, erkdate;
            if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
                bdate = "";
            else
                bdate = txtbdate.Text;

            if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
                edate = "";
            else
                edate = txtedate.Text;

            if (Convert.ToDateTime(txtbjhdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
                sjhdaate = "";
            else
                sjhdaate = Convert.ToDateTime(txtbjhdate.Text).ToString("yyyy-MM-dd");

            if (Convert.ToDateTime(txtejhdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
                ejhdaate = "";
            else
                ejhdaate = Convert.ToDateTime(txtejhdate.Text).ToString("yyyy-MM-dd");
            if (Convert.ToDateTime(txtsdhdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
                sdhdate = "";
            else
                sdhdate = Convert.ToDateTime(txtsdhdate.Text).ToString("yyyy-MM-dd");
            if (Convert.ToDateTime(txtedhdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
                edhdate = "";
            else
                edhdate = Convert.ToDateTime(txtedhdate.Text).ToString("yyyy-MM-dd");
            if (Convert.ToDateTime(txtsrkdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
                srkdate = "";
            else
                srkdate = Convert.ToDateTime(txtsrkdate.Text).ToString("yyyy-MM-dd");
            if (Convert.ToDateTime(txterkdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
                erkdate = "";
            else
                erkdate = Convert.ToDateTime(txterkdate.Text).ToString("yyyy-MM-dd");
            string status = StrSplitParam(txtstatus);
            string season = StrSplitParam(this.txtseason);
            string yearno = StrSplitParam(this.txtyear);
            string matkind = StrSplitParam(this.txtmatkind);
            string sex = StrSplitParam(this.cb_sex);
            DataSet ds = db.DM_GetDateSet("exec KM_OrderTraking_Rpt '" + bdate + "','" + edate
                + "','" + sjhdaate + "','" + ejhdaate + "','" + sdhdate + "','" + edhdate + "','" + srkdate
                + "','" + erkdate + "','" + status + "','" + txtvendor.Value + "','" + season
                + "','" + yearno + "','" + matkind + "','" + txtmatname.Text + "','" + txtmatcode.Text + "',0,'" + sex + "'");
            return ds;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void BtnOutExcel(object sender, DirectEventArgs e)
    {
        try
        {
            DataSet ds = QureyData();
            string path = NpoiHelper.ExportByWeb(mid, ds, this.Title);
            this.ResourceManager1.AddScript("outExcle('" + path + "')");
        }
        catch (Exception ex)
        {
            ShowBox("数据导出出错：" + ex.Message);
        }
    }
    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL 
        string json = this.Hidden1.Value.ToString();
        StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
        XmlNode xml = eSubmit.Xml;

        ExtGridToExcel(mid, "xls", xml, this.Response, this.Title);
    }
    protected void ToExcel(object sender, EventArgs e)
    {
        InsActLog(mid, "导出");
        string json = this.Hidden1.Value.ToString();
        StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
        XmlNode xml = eSubmit.Xml;

        this.Response.Clear();
        this.Response.ContentType = "application/vnd.ms-excel";
        string agent = this.Request.Headers["User-Agent"];
        if (agent != null && (agent.IndexOf("Firefox") >= 0 || agent.IndexOf("Chrome") >= 0 || agent.IndexOf("Safari") >= 0))
        {
            this.Response.AddHeader("Content-Disposition", "attachment; filename=" + this.Title + ".xls");
        }
        else
        {
            this.Response.AddHeader("Content-Disposition", "attachment; filename=dingdanhuoqigenzong.xls");
        }


        XslCompiledTransform xtExcel = new XslCompiledTransform();

        xtExcel.Load(Server.MapPath("Excel.xsl"));
        xtExcel.Transform(xml, null, this.Response.OutputStream);
        this.Response.OutputStream.Close();
        this.Response.OutputStream.Dispose();
        this.Response.End();

    }
}